package com.glory.brepda.activity.mfg;


import android.app.AlertDialog;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;

import com.glory.brepda.adapter.GroupEquTransferStateListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;

import com.glory.brepda.entity.EquCallOutStateEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class EquipmentCallOutMaintainTransferActivity extends BaseActivity implements GroupEquTransferStateListAdapter.NetworkRequestCallback {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.equAddress)
    TextView equAddress;
    @BindView(R.id.rvEqpGroupList)
    RecyclerView rvEqpGroupList;



    private GroupEquTransferStateListAdapter myAdapter;

    private List<EquCallOutStateEntity.ResultBean> groupEquStateList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqm_callouttransfer_change;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("查询");
        initRecyclerView(rvEqpGroupList,this);
        myAdapter= new GroupEquTransferStateListAdapter(null,getSupportFragmentManager(),this,EquipmentCallOutMaintainTransferActivity.this);
        myAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        myAdapter.isFirstOnly(true);
        rvEqpGroupList.setAdapter(myAdapter);
    }

    @Override
    protected void initData() {
        tvBaseSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getEqpStatus();
            }
        });
    }

    private void getEqpStatus() {
        FreeUI_EntityCallBack<BaseResponseBean<EquCallOutStateEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EquCallOutStateEntity>>
                (new TypeToken<BaseResponseBean<EquCallOutStateEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EquCallOutStateEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EquCallOutStateEntity body = response.body().getResponse().getBody();
                    String roleCheckFlag = response.body().getResponse().getBody().getRoleCheckFlag();
                    groupEquStateList.clear();
                    if (!body.getEQUMESSAGELIST().isEmpty()){
                        for (EquCallOutStateEntity.ResultBean entity : body.getEQUMESSAGELIST()){
                            if (!groupEquStateList.contains(entity)){
                                entity.setRoleCheckFlag(roleCheckFlag);
                                groupEquStateList.add(entity);
                            }
                        }
                    }
                    myAdapter.setNewData(groupEquStateList);

                }

            }

            @Override
            public void onError(Response<BaseResponseBean<EquCallOutStateEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEquipment");
            }
        };


        JsonObject map = new JsonObject();
        if(StringUtils.isNoneBlank(etEqpId.getText().toString())){
            map.addProperty("EQUIPMENTID",etEqpId.getText().toString().trim());
        }else{
            map.addProperty("EQUIPMENTID",etEqpId.getText().toString());
        }
        if(StringUtils.isNoneBlank(equAddress.getText().toString())){
            map.addProperty("ADDRESSID",equAddress.getText().toString().trim());
        }else{
            map.addProperty("ADDRESSID",equAddress.getText().toString());
        }

        map.addProperty("ACTIONTYPE","GETSTATUS");
        map.addProperty("ORDERSTATE","CJG");
        NetUtils.requestPostNet("IF.BREEQUCALLOUTMAINTAIN","Common", EquipmentCallOutMaintainTransferActivity.this,map,callBack);
    }

    public   void reciveSubmit(long index){
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","TAKEWORK");
        map.addProperty("OBJECTRRN",index);
        NetUtils.requestPostNet("IF.BREEQUCALLOUTMAINTAIN","Common", EquipmentCallOutMaintainTransferActivity.this,map,null);
    }

    @Override
    public void requestPostNet(long index,String type,String text) {
        FreeUI_EntityCallBack<BaseResponseBean<EquCallOutStateEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EquCallOutStateEntity>>
                (new TypeToken<BaseResponseBean<EquCallOutStateEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EquCallOutStateEntity>> response) {
                getEqpStatus();
            }

            @Override
            public void onError(Response<BaseResponseBean<EquCallOutStateEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "UpdateEquipmentState");
            }
        };
        JsonObject map = new JsonObject();
        if(StringUtils.isNoneBlank(type)){
            map.addProperty("ACTIONTYPE","TRANSFER");//转单
        }
        map.addProperty("OBJECTRRN",index);
        map.addProperty("REMARK",text);
        NetUtils.requestPostNet("IF.BREEQUCALLOUTMAINTAIN","Common", EquipmentCallOutMaintainTransferActivity.this,map,callBack);
    }

    public void showInputDialog(long index,String type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("备注");
        builder.setMessage("请输入备注信息:");

        final EditText input = new EditText(this);
        builder.setView(input);

        builder.setPositiveButton("提交", (dialog, which) -> {
            String text = input.getText().toString();
            if(StringUtils.isEmpty(text)){
                Toast.makeText(EquipmentCallOutMaintainTransferActivity.this, "请输入文字", Toast.LENGTH_SHORT).show();
                return;
            }else{
                requestPostNet(index,type,text);
            }
        });

        builder.setNegativeButton("取消", (dialog, which) -> dialog.cancel());
        builder.show();

    }
}
